package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.wfx.entity.*;
import com.wfx.mapper.WxbGoodsBrandMapper;
import com.wfx.mapper.WxbGoodsMapper;
import com.wfx.service.IWxbGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-21
 */
@Service
public class WxbGoodsServiceImpl extends ServiceImpl<WxbGoodsMapper, WxbGoods> implements IWxbGoodsService {

    @Autowired
    private WxbGoodsMapper wxbGoodsMapper;

    //分页加条件
    @Override
    public PageResult<WxbGoods> search(PageDTO<WxbGoods> pageDTO) {
        if (pageDTO == null) {
            return new PageResult<WxbGoods>(false,"查询失败",null,null,null);
        }
        IPage<WxbGoods> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        QueryWrapper<WxbGoods> queryWrapper = new QueryWrapper<>();
        PageResult pageResult = new PageResult();
        WxbGoods data = pageDTO.getData();
        System.out.println(data);
        System.out.println(pageDTO);
        if (!StringUtils.isEmpty(data.getGoodsName())) {
            queryWrapper.eq("goods_name",data.getGoodsName());
        }
        IPage<WxbGoods> pageInfo = wxbGoodsMapper.selectPage(iPage, queryWrapper);
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }

    //商户系统分页加条件
    @Override
    public PageResult<WxbGoods> searchMerchant(PageDTO<WxbGoods> pageDTO) {
        try {
            if (pageDTO == null) {
                return new PageResult<WxbGoods>(false,"查询失败");
            }
            IPage<WxbGoods> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
            QueryWrapper<WxbGoods> queryWrapper = new QueryWrapper<>();
            PageResult pageResult = new PageResult();
            WxbGoods data = pageDTO.getData();

            String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
            PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);
            //解析数据
            WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(data.getSellerId(), publicKey, WxbCustomer.class);
            data.setSellerId(wxbCustomer.getCustomerId());

            if (!StringUtils.isEmpty(data.getSellerId())) {
                queryWrapper.eq("seller_id",data.getSellerId());
            }
            if (!StringUtils.isEmpty(data.getGoodsName())) {
                queryWrapper.eq("goods_name",data.getGoodsName());
            }
            IPage<WxbGoods> pageInfo = wxbGoodsMapper.selectPage(iPage, queryWrapper);
            pageResult.setSuccess(true);
            pageResult.setData(pageInfo.getRecords());
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setMsg("查询成功");
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    //自媒体系统分页加条件
    @Override
    public PageResult<WxbGoods> searchMedia(PageDTO<WxbGoods> pageDTO) {
        try {
            if (pageDTO == null) {
                return new PageResult<WxbGoods>(false,"查询失败");
            }
            IPage<WxbGoods> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
            QueryWrapper<WxbGoods> queryWrapper = new QueryWrapper<>();
            PageResult pageResult = new PageResult();
            if(pageDTO.getData()!=null){
                WxbGoods data = pageDTO.getData();
                if (!StringUtils.isEmpty(data.getCategory1Id())) {
                    queryWrapper.eq("category1_id",data.getCategory1Id());
                }
                if (!StringUtils.isEmpty(data.getCategory2Id())) {
                    queryWrapper.eq("category2_id",data.getCategory2Id());
                }
                if (!StringUtils.isEmpty(data.getCategory3Id())) {
                    queryWrapper.eq("category3_id",data.getCategory3Id());
                }
                if (!StringUtils.isEmpty(data.getBrandId())) {
                    queryWrapper.eq("brand_id",data.getBrandId());
                }
                if (!StringUtils.isEmpty(data.getSellerId())) {
                    queryWrapper.eq("seller_id",data.getSellerId());
                }
                if (!StringUtils.isEmpty(data.getGoodsName())) {
                    queryWrapper.like("goods_name",data.getGoodsName());
                }
            }
            IPage<WxbGoods> pageInfo = wxbGoodsMapper.selectPage(iPage, queryWrapper);
            pageResult.setSuccess(true);
            pageResult.setData(pageInfo.getRecords());
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setMsg("查询成功");
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
    //商品详情
    @Override
    public WxbGoods selectByIdOne(String id) {
        return wxbGoodsMapper.selectByIdOne(id);
    }


    //修改状态
    @Override
    public Result update(WxbGoods wxbGoods) {
        //wxbGoodsMapper.selectById(wxbGoods.getId());
        if (wxbGoods == null) {
            return new Result(false,"参数异常");
        }
        int i = wxbGoodsMapper.updateById(wxbGoods);
        return new Result(i==1?true:false,i==1?"修改成功":"修改失败");
    }

    @Override
    public List<WxbGoods> findByCustomerId(WxbGoods wxbGoods) {
        if (wxbGoods == null) {
            return null;
        }
        try {

        String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
        PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);

        //解析数据
        WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(wxbGoods.getSellerId(), publicKey, WxbCustomer.class);
        QueryWrapper<WxbGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_id",wxbCustomer.getCustomerId());
        return wxbGoodsMapper.selectList(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



    @Override
    public Result insertGood(WxbGoods wxbGoods) {
        if (wxbGoods == null) {
            return new Result(false,"参数异常");
        }




        wxbGoods.setIsDelete("否");
        wxbGoods.setIsEnableSpec("是");
        if(!StringUtils.isEmpty(wxbGoods.getId())){
            int i = wxbGoodsMapper.updateById(wxbGoods);
            return  new Result(i==1?true:false,i==1?"修改成功":"修改失败");
        }else {

            try {
                String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
                PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);

                //解析数据
                WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(wxbGoods.getSellerId(), publicKey, WxbCustomer.class);
                wxbGoods.setSellerId(wxbCustomer.getCustomerId());
                wxbGoods.setAuditStatus("2");
                int i = wxbGoodsMapper.save(wxbGoods);
                return  new Result(i==1?true:false,i==1?"添加成功":"添加失败",wxbGoods.getId());
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    }


    //删除商品
    @Override
    public Result delOne(WxbGoods wxbGoods) {
        int i = wxbGoodsMapper.deleteById(wxbGoods);
        return new Result(i==1?true:false,i==1?"删除成功":"goods删除失败");
    }
    //根据商品id查询商品信息
    @Override
    public Result findGoods(String goodsId) {
        WxbGoods wxbGoods = wxbGoodsMapper.selectById(goodsId);
        if (wxbGoods == null) {
            return new Result(false,"数据为空");
        }
        return new Result(true,"查询成功",wxbGoods);
    }
}
